﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;

namespace Cloak.Linq
{
	/// <summary>
	/// Extends sequences with the ability to use them as the source of a <see cref="ReadOnlyCollection{T}"/>
	/// </summary>
	public static class ReadOnlySequence
	{
		/// <summary>
		/// Gets a read-only collection populated with the elements of the specified sequence
		/// </summary>
		/// <param name="source">The sequence used to populate the <see cref="ReadOnlyCollection{T}"/></param>
		/// <returns>A collection which cannot be modified</returns>
		[Pure]
		public static ReadOnlyCollection<T> ToReadOnly<T>(this IEnumerable<T> source)
		{
			Contract.Requires(source != null);

			return source as ReadOnlyCollection<T> ?? new ReadOnlyCollection<T>(source.ToList());
		}
	}
}